package com.heima.behavior.advice;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.dto.BehaviorDto;
import com.heima.behavior.dto.UpdateArticleMessage;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Aspect
public class
BehaviorAdvice {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Around("execution(* com.heima.behavior.service.impl.*.*(..))")
    public Object recordToRedis(ProceedingJoinPoint pjp) throws Throwable{
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Object[] args = pjp.getArgs();
        String methodName = signature.getMethod().getName();
        // 方法名以save开头的
        if(methodName.startsWith("save")){
            log.info("[AOP======>方法接收参数：{}]",signature.getMethod().getParameters());
            BehaviorDto dto = (BehaviorDto)args[0];
            if(dto.getOperation()==0){
                // 这里需要用户id已在Controller方法中赋值
                redisTemplate.boundValueOps(methodName+"_"+dto.getUserId()+"_"+dto.getEquipmentId()+"_"+dto.getArticleId()).set("redis");
                // set设置什么无所谓，但是如果是空，在可视化工具RDM中会报格式化错误，但是hasKey是没问题的

                // 操作类型 0 阅读saveRead 1 点赞saveLikes 2 评论 3 收藏saveCollection
                // 向kafka发送数据 添加1
                sendMessageToKafka(methodName,dto.getArticleId(),1);
            }else{
                // 向kafka发送数据 减少-1
                sendMessageToKafka(methodName,dto.getArticleId(),-1);
                redisTemplate.delete(methodName+"_"+dto.getUserId()+"_"+dto.getEquipmentId()+"_"+dto.getArticleId());
            }
        }
        return pjp.proceed(args);
    }

    private void sendMessageToKafka(String methodName, Long articleId, int i) {
        // 操作类型 0阅读saveRead   1点赞saveLikes    2评论     3收藏saveCollection
        UpdateArticleMessage message = new UpdateArticleMessage();
        // 阅读 0
        if (methodName.equals("saveRead")) {
            message.setType(0);
            message.setArticleId(articleId);
            message.setAdd(i);
        }
        // 点赞 1
        if (methodName.equals("saveLikes")) {
            message.setType(1);
            message.setArticleId(articleId);
            message.setAdd(i);
        }
        // 收藏 3
        if (methodName.equals("saveCollection")) {
            message.setType(3);
            message.setArticleId(articleId);
            message.setAdd(i);
        }
        // 向kafka发送数据
        kafkaTemplate.send("hot_article_score_topic", JSON.toJSONString(message));

        // 评论 2
    }

}
